package cn.kgc.easybuy.controller;

import cn.kgc.easybuy.annotaion.CheckPermission;
import cn.kgc.easybuy.pojo.News;
import cn.kgc.easybuy.service.NewsService;
import cn.kgc.easybuy.utils.AjaxResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class NewsController {

    @Autowired
    private NewsService newsService;

    @CheckPermission({"1"})
    @GetMapping("/delNews")
    @ApiOperation(value = "删除资讯")
    @ApiImplicitParam(name = "id",value = "删除资讯id",required = true)
    public AjaxResult delNews(Integer id){
        if (id == null){
            throw new RuntimeException("删除资讯id为空");
        }
        boolean flag = newsService.delNews(id);
        if ( flag){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @CheckPermission({"1"})
    @PostMapping("/updateNews")
    @ApiOperation(value = "修改资讯")
    @ApiImplicitParam(name = "news",value = "修改资讯对象",required = true)
    public AjaxResult updateNews(@RequestBody News news){
        if (news == null){
            throw new RuntimeException("修改资讯为空");
        }
        boolean flag = newsService.updateNews(news);
        if (flag){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @CheckPermission({"1"})
    @PostMapping("/addNews")
    @ApiOperation(value = "添加资讯")
    @ApiImplicitParam(name = "news",value = "添加资讯对象",required = true)
    public AjaxResult addNews(@RequestBody News news){
        if (news == null){
            throw new RuntimeException("添加资讯为空");
        }
        boolean flag = newsService.addNews(news);
        if (flag){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/selectNewsById")
    @ApiOperation(value = "查询资讯")
    @ApiImplicitParam(name = "id",value = "查询资讯id",required = true)
    public AjaxResult selectNewsById(Integer id){
        if (id == null){
            throw new RuntimeException("查询资讯id为空");
        }
        News news = newsService.selectNewsById(id);
        if (news != null){
            return AjaxResult.success().add("newsInfo",news);
        }
        return AjaxResult.error();
    }

    @PostMapping("/selectNewsByPage")
    @ApiOperation(value = "分页查询资讯")
    @ApiImplicitParam(name = "params",value = "分页查询参数：pageNum,pageSize,title(可选择)",required = true)
    public AjaxResult selectNewsByPage(@RequestBody Map<String, Object> params){
        return AjaxResult.success().add("pageInfo",newsService.selectNewsByPage(params));
    }
}
